package org.presstrack.vos
{
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	
	import org.presstrack.service.CrudService;
	
	import presstrack.TaskClass;
	
	[RemoteClass(alias="org.presstrack.vos.TrackVO")]
	[Bindable]
	public class TrackVO
	{
		public var label:String;
		public var manager:int;
		public var manager_name:String;
		public var customer:int;
		public var customer_name:String;
		public var tasks:ArrayCollection;
		
		public static var TRACKS:ArrayCollection;
		
		public static var allSections:ArrayCollection = new ArrayCollection([
        	{section:2, label:"бумага"},
        	{section:3, label:"препресс"},
        	{section:4, label:"печать"},
        	{section:5, label:"постпечать"},
       		{section:6, label:"отгрузка"} 
        ]);        
        public static var allManagers:ArrayCollection;
        public static var allCustomers:ArrayCollection;
        public static var allOperations:ArrayCollection;
        
        public static var curSections:ArrayCollection = new ArrayCollection;
		public static var curManagers:ArrayCollection  = new ArrayCollection;
		public static var curCustomers:ArrayCollection = new ArrayCollection;
		public static var curOperations:ArrayCollection = new ArrayCollection;
		public static var curOrder:int;        
        
		
		public function TrackVO()
		{		
					
		}
		
		public static function setFrom(res:Object):void {			
			TrackVO.TRACKS = new ArrayCollection;
			for each(var _order:Object in res.track) {
				var tr:TrackVO = new TrackVO;
				tr.label = _order.label;
				tr.manager = _order.manager;
				tr.manager_name = _order.manager_name;
				tr.customer = _order.customer;
				tr.customer_name = _order.customer_name;
				tr.tasks = new ArrayCollection();
				for each(var t:Object in _order.tasks) {
					var task:TaskClass = new TaskClass(t);
					tr.tasks.addItem(task);
				}
				
				var sortTask:Sort = new Sort;
        		sortTask.fields=[new SortField("section", true)];
				tr.tasks.sort = sortTask;
				
				if (tr.tasks.length > 0)
					TrackVO.TRACKS.addItem(tr);
			}
			
			TrackVO.allManagers = new ArrayCollection(res.allManagers);
			TrackVO.allManagers.filterFunction = TrackVO.filterForAllManagers;
			TrackVO.allManagers.refresh();
			
        	TrackVO.allCustomers = new ArrayCollection(res.allCustomers);
        	TrackVO.allCustomers.filterFunction = TrackVO.filterForAllCustomers;
        	TrackVO.allCustomers.refresh();
        	
        	TrackVO.allOperations = new ArrayCollection(res.allOperations);
			TrackVO.allOperations.filterFunction = TrackVO.filterForAllOperations;
			TrackVO.allOperations.refresh();
			
			TrackVO.allSections.filterFunction = TrackVO.filterForAllSections;
			TrackVO.allSections.refresh(); 
			
			TrackVO.TRACKS.filterFunction = TrackVO.filterForTrack;
			TrackVO.TRACKS.refresh();	
			
			CrudService.isTrackLoad = true;								
		}
		
		public static function filterForAllSections(it:Object) : Boolean {
			return (TrackVO.curSections.contains(it)) ? false : true;
		}
		
		public static function filterForAllOperations(it:Object) : Boolean {
			if (TrackVO.curOperations.contains(it)) return false;
			if (TrackVO.curSections.length < 1) return true;
			var res:Boolean;
			for each (var sec:Object in TrackVO.curSections){
				if (sec.section == it.section){
					res = true;
					break;
				}
			}
			if (res == false) return false;
			return true;
		}
		
		public static function filterForAllManagers(it:Object) : Boolean {
			return (TrackVO.curManagers.contains(it)) ? false : true;
		}
		
		public static function filterForAllCustomers(it:Object) : Boolean {
			if (TrackVO.curCustomers.contains(it)) return false;
			if (TrackVO.curManagers.length < 1) return true;
			var res:Boolean;
			for each (var man:Object in curManagers){
				if (man.iduser == it.medium){
					res = true;
					break;
				}
			}
			if (res == false) return false;
			return true;
		}
		
		public static function filterForTrack(it:Object) : Boolean {
			var res:Boolean;
			// фильтрация по менеджерам
			if (TrackVO.curManagers.length > 0) {
				res = false;
				for each(var man:Object in TrackVO.curManagers){
					if (man.iduser == it.manager){
						res = true;
						break;
					}
				}
				if (res == false) return false;
			}
			
			
			// фильтрация по заказчикам
			if (TrackVO.curCustomers.length > 0) {
				res = false;
				for each(var cust:Object in TrackVO.curCustomers){
					if (cust.iduser == it.customers){
						res = true;
						break;
					}
				}
				if (res == false) return false;
			}
			
			// фильтрация по секциям и операциям
			it.tasks.filterFunction = TrackVO.filterForTask;
			it.tasks.refresh();
			if (it.tasks.length < 0) return false;	
			
			for each(var sec:Object in TrackVO.curSections) {
				var count:int = 0;
				for each(var tsk:Object in it.tasks)
				{
					if (sec.section == tsk.section)
						++count;
				}
				if (count < 1) return false;
			}
			
			/*for each(var op:Object in TrackVO.curOperations)
			{
				res = false;
				for each(var tsk:Object in it.tasks)
				{
					if (Number(op.section) == Number(tsk.section))
					{
						if ( op.section == 2) // если бумага
						{
							res = true;
							break;
						}
						else if (op.section == 4) // если печать
						{
							if (tsk.value.search(op.label) > 0)
							{
								res = true;
								break;
							}
						}
						else // если препресс, постпечать
						{
							if (op.label == tsk.label)
							{
								res = true;
								break;
							}
						}
					}  
				}
				if (res == false) return false;				
			}*/			
			return true;
		}
		
		public static function filterForTask(tsk:Object):Boolean {
			var res:Boolean = new Boolean(false);
			
			if (TrackVO.curSections.length < 1) return true;
			
			for each(var sec:Object in TrackVO.curSections){
				if (Number(sec.section) == Number(tsk.section)){
					if (TrackVO.curOperations.length < 1) return true; // если список с операциями пуст 
					else if ( sec.section == 2) // если бумага
					{
						return true;
					}
					else if (sec.section == 4) // если печать
					{
						for each(var op:Object in TrackVO.curOperations){
							if (op.section == sec.section && tsk.value.search(op.label) > 0){
								return true;
							}
						}
					}
					else // если препресс, постпечать
					{						
						for each(var op2:Object in TrackVO.curOperations){
							if (op2.label == tsk.label){
								return true;
							}
						}					
					}  
				}				
			}
			return false;
		}			
	}
}